﻿Imports System.IO
Imports System.IO.Ports.SerialPort
Public Class Advia1650

    'Public Structure stResult
    '    Dim FirstComment As String
    '    Dim SecondComment As String
    '    Dim Sex As String
    '    Dim Age As Int16
    '    Dim PID As String
    '    Dim SID As String
    '    Dim TestResult() As String
    '    Dim TestName() As String
    '    Dim TestDate As String

    'End Structure
#Region "Structures"
    'Public Structure ResultAspectStruct
    '    Public Aspect As String
    '    Public DataValue As String
    '    Public Unit As String
    'End Structure
    Public Structure ResultStruct
        Dim TestCode As String
        Dim DataValue As String
    End Structure
    Public Structure Patient
        Dim PatientName() As String
        Dim Age As String
        Dim Sex As String
        Dim DateOfBirth As String
        Dim PhysicianID As String
        Dim Location As String
        Dim PID As String
        Dim SID As String
        Dim DateTestCompleted As String
        Dim ResultArray() As ResultStruct
        Dim Comment As String
    End Structure
#End Region

    Private Const FrameDelimiter As Char = vbCrLf
    Public Const ETB As Byte = &H17
    Public Const OrderRecordType As String = "O"
    Private Const ResultRecordType As String = "R"



    Public Function FrameCheck(ByVal sFrameInput As String, ByRef FinalFrame As Boolean, ByRef sDataSegmentOutput As String) As Boolean
        Dim checksum1, checksum2 As Byte
        Dim sTemp As String
        checksum1 = Convert.ToByte(sFrameInput.Substring(sFrameInput.Length - 2), 16)
        sTemp = sFrameInput.Remove(sFrameInput.Length - 2)
        sTemp = sTemp.Substring(1)
        checksum2 = StringChecksum(sTemp)
        If checksum2 <> checksum1 Then
            Return False
        Else
            sDataSegmentOutput = sTemp.Remove(sTemp.Length - 1)     'Loc bo ETX hoac ETB
            sDataSegmentOutput = sDataSegmentOutput.Substring(1)            'Loc bo Frame No.
            If Asc(Convert.ToChar(sTemp.Substring(sTemp.Length - 1))) = ETB Then
                FinalFrame = False
            Else
                FinalFrame = True
                sDataSegmentOutput &= vbCr
            End If
            Return True
        End If
    End Function

    Public Function GetQuery(ByVal sDataSegment As String, ByRef StackOutput As StackStruct, ByRef NoOfTest As Byte) As Boolean
        Dim sRecordTemp As String()
        Dim sTemp1, sTemp2 As String()
        Dim s As String
        Dim NotQuery As Boolean = True

        If sDataSegment.Substring(0, 1) = "Q" Then

            NoOfTest = Val(sDataSegment.Substring(6, 2))
            StackOutput.StackPointer = 0
            ReDim StackOutput.sBarcodeStack(NoOfTest - 1)

            For i As Byte = 0 To NoOfTest - 1
                StackOutput.sBarcodeStack(i) = sDataSegment.Substring(9 + i * 13, 13)
            Next i
            If NotQuery Then
                NotQuery = False
            End If

        End If

        Return (Not NotQuery)
    End Function
    Public Function GetResult(ByVal sDataSegment As String, ByRef sRecordOutput() As String) As Boolean

        Dim NotResult As Boolean = True
        sRecordOutput = sDataSegment.Split(vbCr)
        For Each s As String In sRecordOutput
            If s.Substring(0, 1) = ResultRecordType Then
                If NotResult Then
                    NotResult = False
                End If
                Exit For
            End If
        Next
        Return (Not NotResult)
    End Function

    'Public Function GetTerminationStatus(ByVal sDataSegment As String) As Boolean
    '    Dim sRecordTemp As String()
    '    Dim NotEnd As Boolean = True
    '    Dim sTemp1 As String()
    '    sRecordTemp = sDataSegment.Split(RecordDelimiter)
    '    For Each s As String In sRecordTemp
    '        sTemp1 = s.Split(FieldDelimiter)
    '        If sTemp1(0) = "L" Then
    '            'If sTemp1(2).Length > 0 And sTemp1(2) = "F" Then
    '            NotEnd = False
    '            Exit For
    '            'End If
    '        End If
    '    Next
    '    Return (Not NotEnd)
    'End Function

    Public Function CreatOrderRecord(ByVal SeqNo As Byte, ByVal SID As String, ByVal TestID As String()) As String
        Dim sTemp() As String
        Dim i As Byte
        ReDim sTemp(15 + TestID.GetLength(0))
        If SeqNo > 0 Then
            sTemp(0) = SeqNo.ToString("D01")
        Else
            MsgBox("Error In Sequence Number")
            Return [String].Empty
        End If
        sTemp(1) = OrderRecordType
        sTemp(2) = " "
        'sTemp(3) = NoOfBlock.ToString("D02")
        sTemp(3) = "01"
        sTemp(5) = TestID.Length.ToString("D03")
        'If BlockNo <> 1 Then
        '    'sTemp(3) = NoOfBlock.ToString("D02")
        '    sTemp(4) = BlockNo.ToString("D02")
        '    sTemp(6) = " "
        '    sTemp(7) = " "
        '    sTemp(9) = Space(7)
        '    sTemp(10) = String.Empty
        '    sTemp(11) = String.Empty
        '    sTemp(12) = String.Empty
        '    sTemp(13) = String.Empty
        'Else
        'sTemp(3) = "01"
        sTemp(4) = "01"
        sTemp(6) = "N"
        sTemp(7) = "1"
        sTemp(9) = [String].Concat(Space(7), Space(16), Space(16))
        sTemp(10) = "M"
        sTemp(11) = [String].Concat(Space(3), Space(8), " 1.0")
        sTemp(12) = "1"
        sTemp(13) = "1"
        'End If
        sTemp(8) = SID


        For i = 0 To TestID.GetLength(0) - 1
            sTemp(14 + i) = [String].Concat(TestID(i), "M")
        Next
        i = sTemp.GetLength(0) - 1
        sTemp(i) = " "
        Dim s As String = [String].Concat([String].Concat(sTemp))
        Return s
    End Function
    

    Private Function StringChecksum(ByVal sInput As String) As Byte
        Dim bSTemp() As Char
        Dim bChecksum As Int16 = 0
        bSTemp = sInput.ToCharArray()
        For Each CharinArr As Char In bSTemp
            bChecksum += Asc(CharinArr)
            bChecksum = bChecksum Mod &H100
        Next
        Return bChecksum
    End Function



    Public Sub ResultDataParse(ByVal sInput As String, ByRef PatientStructure As Patient)
        
        Dim NoOfTest As Int16 = 0
        sInput = sInput.Remove(0, 6)
        NoOfTest = Val(sInput.Substring(0, 3))

        With PatientStructure
            ReDim .ResultArray(NoOfTest - 1)
            .DateTestCompleted = sInput.Substring(3, 8)
            .SID = sInput.Substring(13, 13).Trim
            For i As Int16 = 0 To NoOfTest - 1
                With .ResultArray(i)
                    .TestCode = sInput.Substring(83 + i * 15, 3)
                    .DataValue = sInput.Substring(83 + i * 15 + 4, 8).Trim
                End With
            Next i

        End With

    End Sub
    Public Function CreateSinglePatientFrame(ByVal OrderRecord As String, ByVal BlockNo As Byte, ByVal NoOfBlock As Byte) As String
        Dim sTemp, sChecksum As String
        Dim Checksum As Byte = 0
        If BlockNo = NoOfBlock Then
            sTemp = [String].Concat(Chr(STX), OrderRecord, Chr(ETX))
        Else
            sTemp = [String].Concat(Chr(STX), OrderRecord, Chr(ETX))
        End If
        Checksum = StringChecksum(sTemp.Substring(1))
        sChecksum = Checksum.ToString("X02")
        'sTemp = sTemp & sChecksum & vbCrLf
        Return [String].Concat(sTemp, sChecksum, vbCrLf)
    End Function

    'Function spSeparateData(ByVal strInput As String) As stResult
    '    Dim TestCount, iCount As Int16
    '    Dim pTemp As String
    '    Dim vTempResult As stResult

    '    ' 0: STX; 1: FNum; 2:R; 3: Dau trang; 4-5: So block; 6-7: BlockNumber; 8-10: so luong Test
    '    '11-18:Date; 19: Sample Class; 20: ID spec; 21-33: Barcode; 34-40: Tray, cup
    '    '41-56: Comment1; 57-72: Comment2; 73: SEX; 74-76: AGE; 77-84: SDate;
    '    ' 85-88: dau phay; 89: SClass; 90: containt Class; 

    '    strInput = strInput.Substring(0, strInput.Length - 3) ' Bo 3 ky tu checksum
    '    pTemp = strInput.Substring(8, 3)
    '    TestCount = Me.TrimZeroData(pTemp)
    '    If TestCount > 0 Then
    '        ReDim vTempResult.TestResult(TestCount - 1)
    '        ReDim vTempResult.TestName(TestCount - 1)
    '    End If

    '    vTempResult.TestDate = strInput.Substring(17, 2) & "/" & strInput.Substring(15, 2) & "/" & strInput.Substring(11, 4)
    '    vTempResult.SID = strInput.Substring(21, 13).Trim
    '    vTempResult.FirstComment = strInput.Substring(41, 16).Trim
    '    vTempResult.SecondComment = strInput.Substring(57, 16).Trim
    '    'vTempResult.SecondComment = strInput.Substring(57, 8).Trim
    '    vTempResult.Sex = strInput.Substring(73, 1)
    '    'vTempResult.Sex = strInput.Substring(65, 1)
    '    pTemp = strInput.Substring(74, 3).Trim
    '    'pTemp = strInput.Substring(66, 3).Trim
    '    If pTemp.Length > 0 Then
    '        vTempResult.Age = Me.TrimZeroData(pTemp)
    '    Else
    '        vTempResult.Age = 0
    '    End If

    '    strInput = strInput.Substring(91)
    '    'strInput = strInput.Substring(83)
    '    'strInput = strInput.TrimStart(" ")
    '    'strInput = strInput.TrimEnd(" ")

    '    iCount = 0
    '    Do While iCount < TestCount
    '        pTemp = strInput.Substring(0, 4)
    '        strInput = strInput.Substring(4)
    '        vTempResult.TestName(iCount) = pTemp.Substring(0, 3)
    '        pTemp = strInput.Substring(0, 8)
    '        strInput = strInput.Substring(11)
    '        vTempResult.TestResult(iCount) = pTemp.Trim
    '        iCount = iCount + 1
    '        'strInput = strInput.TrimStart(" ")
    '        If strInput.Substring(0, 1) = "R" Then
    '            strInput = strInput.Substring(1)
    '        End If
    '    Loop

    '    Return vTempResult

    'End Function

    'Private Function TrimZeroData(ByVal strInput As String) As Integer
    '    Try
    '        Do While strInput.Substring(0, 1) = "0" And strInput.Length > 1
    '            If strInput.Substring(1, 1) = "0" Then
    '                strInput = strInput.Remove(0, 1)
    '            Else
    '                Exit Do
    '            End If
    '        Loop
    '        Return CInt(strInput)
    '    Catch ex As Exception
    '        Return 0
    '    End Try

    'End Function

    'Private Function TrimData(ByVal strInput As String, ByVal strTrim As String) As String
    '    Try
    '        Do While strInput.Substring(0, strTrim.Length) = strTrim And strInput.Length > 1
    '            If strInput.Substring(1, strTrim.Length) = strTrim Then
    '                strInput = strInput.Remove(0, strTrim.Length)
    '            Else
    '                Exit Do
    '            End If
    '        Loop
    '        Return strInput
    '    Catch ex As Exception
    '        Return ""
    '    End Try

    'End Function


End Class
